/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/itframe">itframe</a> All rights reserved.
 */
package com.rise.itframe.modules.inv.web;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.rise.itframe.common.config.Global;
import com.rise.itframe.common.persistence.Page;
import com.rise.itframe.common.utils.excel.ExportExcel;
import com.rise.itframe.common.web.BaseController;
import com.rise.itframe.modules.inv.entity.Review;
import com.rise.itframe.modules.inv.service.ReviewService;
import com.rise.itframe.modules.sys.utils.DictUtils;
import com.rise.itframe.modules.sys.utils.UserUtils;

/**
 * 评审登记信息Controller
 * @author ming
 * @version 2017-12-31
 */
@Controller
@RequestMapping(value = "${adminPath}/inv/review")
public class ReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    @ModelAttribute
    public Review get(@RequestParam(required = false) String id) {
        Review entity = null;
        if (org.apache.commons.lang3.StringUtils.isNotBlank(id)){
            entity = reviewService.get(id);
        }
        if (entity == null){
            entity = new Review();
        }
        return entity;
    }

    // @RequiresPermissions("inv:review:view")
    @RequestMapping(value = {"list", ""})
    public String list(Review review, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<Review> page = reviewService.findPage(new Page<Review>(request, response), review);
        model.addAttribute("page", page);

        if ("9".equals(review.getOperateFlag())) {// 查询统计
            Map<String, String> sumMap = reviewService.findListSum(review);
            model.addAttribute("sumMap", sumMap);
            return "modules/inv/reviewSearchList";
        }
        return "modules/inv/reviewList";
    }



    // @RequiresPermissions("inv:review:view")
    @RequestMapping(value = "form")
    public String form(Review review, HttpServletRequest request, Model model) {

        if (review.getIsNewRecord()) {
            review.setStatus("0");
            review.setCreateBy(UserUtils.getUser());
            review.setCreateDate(new Date());
            review.setReviewDate(new Date());
        }
        model.addAttribute("review", review);
        return "modules/inv/reviewForm";
    }

    // @RequiresPermissions("inv:review:edit")
    @RequestMapping(value = "save")
    public String save(Review review, Model model, HttpServletRequest request,
        RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, review)) {
            return form(review, request, model);
        }
        String names = request.getParameter("names");
        reviewService.save(review, names);
        addMessage(redirectAttributes, "保存评审信息成功");
        redirectAttributes.addFlashAttribute("rtnCode", "0");
        return "redirect:" + Global.getAdminPath() + "/inv/review/form?repage";
    }

    // @RequiresPermissions("inv:review:edit")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(Review review, RedirectAttributes redirectAttributes) {
        reviewService.delete(review);
        return "success";
    }

    /**
     * 导出查询列表
     *
     * @param dttColorPrintFileInfo
     * @param request
     * @param response
     * @param redirectAttributes
     * @param model
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "export", method = RequestMethod.POST)
    @ResponseBody
    public String exportExcel(Review review, HttpServletRequest request, HttpServletResponse response,
        RedirectAttributes redirectAttributes, Model model) throws IOException {
        try {
            Page<Review> page = reviewService.findPage(new Page<Review>(request, response, -1), review);
            model.addAttribute("page", page);

            Map<String, String> sumMap = reviewService.findListSum(review);
            model.addAttribute("sumMap", sumMap);

            List<String> headerList = Lists.newArrayList();
            headerList.add("评审名称");
            headerList.add("评审日期");
            headerList.add("任务号");
            headerList.add("评审金额");
            headerList.add("总金额");
            headerList.add("评审人数");
            headerList.add("评审人员");
            headerList.add("经办人");
            headerList.add("录入人");
            headerList.add("录入时间");

            ExportExcel ee = new ExportExcel("评审信息", headerList);
            int index = 0;
            for (int i = 0; i < page.getList().size(); i++) {
                index = 0;
                Row row = ee.addRow();
                Review data = page.getList().get(i);
                ee.addCell(row, index++, data.getReviewName());
                ee.addCell(row, index++, data.getReviewDate());
                ee.addCell(row, index++, DictUtils.getDictLabel(data.getTaskNo(), "INV_TASK_NO", data.getTaskNo()));
                ee.addCell(row, index++, data.getReviewFee());
                ee.addCell(row, index++, data.getReviewNum());
                ee.addCell(row, index++, data.getReviewTotalFee());
                ee.addCell(row, index++, data.getReviewUserName());
                ee.addCell(row, index++, data.getTransactName());
                ee.addCell(row, index++, data.getCreateBy().getName());
                ee.addCell(row, index++, data.getCreateDate());
            }

            index = 0;
            Row row = ee.addRow();
            ee.addCell(row, index++, "汇总");
            ee.addCell(row, index++, "");
            ee.addCell(row, index++, sumMap.get("REVIEWFEE"));
            ee.addCell(row, index++, sumMap.get("REVIEWNUM"));
            ee.addCell(row, index++, sumMap.get("REVIEWTOTALFEE"));
            ee.addCell(row, index++, "");
            ee.addCell(row, index++, "");
            ee.addCell(row, index++, "");
            ee.write(response, "评审信息导出" + new Date().getTime() + ".xlsx");
            return null;

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("导出失败！");
        }
        return null;
    }

}